package engineer.seniordesign.cynogriffin;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.androidVNC.VNC;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class LinkFileListActivity extends ListActivity {
    public ArrayList<String> programNames;
    private String host, username, password;
    private int port;
    private SharedPreferences settings;
    private VNC vnc;
    private String[] fileNames;
    private Semaphore mutexVNC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {		
    	super.onCreate(savedInstanceState);

    	settings 	= getSharedPreferences("settings", MODE_PRIVATE);
    	host 		= settings.getString("prefHost", null);
    	port 		= Integer.parseInt(settings.getString("prefPort", null));
    	username 	= settings.getString("prefUsername", null);
    	password 	= settings.getString("prefPassword", null); 
    	
    	mutexVNC = new Semaphore(1);

    	VNCThread threadVNC = new VNCThread();

    	try {
			mutexVNC.acquire();
		} catch (InterruptedException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
    	threadVNC.start();

        
        programNames = new ArrayList<String>();
        
        
        try {
        	mutexVNC.acquire();
        	try {
        		for(int i = 0; i < fileNames.length; i++) {
        			programNames.add(fileNames[i]);
        		}
        	} finally {
        		mutexVNC.release();
        	}

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
        
        
        if (programNames.isEmpty()){
        	programNames.add("No programs");
        }
        
        ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
        		android.R.id.text1, programNames);
        setListAdapter(mAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_link_file_list, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.quit:
	        	// TODO Close RDP connections
	        case R.id.settings:
	        	Intent i = new Intent(getApplicationContext(), SettingsActivity.class);
	        	startActivity(i);
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	 
    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) {
    	Thread t = new Thread() {
    		@Override
    		public void run() {
    			vnc.closeFTPCConnection();
    		}
    	};
    	t.start();
    	Object selectedApp = l.getItemAtPosition(position);
    	Intent i = new Intent(getApplicationContext(), AppViewActivity.class);
    	i.putExtra("AppName", selectedApp.toString());
    	startActivity(i);
    }
    
    private class VNCThread extends Thread {
    	@Override 
    	public void run(){
    		try{
    			vnc = new VNC(host, port, username, password);

    			vnc.connectToVNC();

    			if(vnc.connectToFTP()){
    				Log.d("msg", "ftp connection successful");

    				//get the list of all the files available
    				fileNames = vnc.listAvailableFiles();
    				if(fileNames != null){
    					for(int i = 0; i < fileNames.length; i++){
    						Log.d("msg", fileNames[i]);
    					}
    				}

    			} else {
    				Log.d("msg", "something failed");
    			}

				mutexVNC.release();

    		} catch(Exception e2){
    			Log.d("msg",e2.toString());
    		}

    	}
    }

}
